package org.orbis.term.chopper.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.java.sen.SenFactory;
import net.java.sen.StringTagger;
import net.java.sen.dictionary.Token;
import org.orbis.term.Term;
import org.orbis.term.TermList;
import org.orbis.term.chopper.Chopper;
import org.orbis.term.tool.StringConverter;

public class GosenChopper implements Chopper{
	@Override
	public TermList chop(String content){
		content = StringConverter.convert(content);

		if(content == null) return null;
		StringTagger tagger = null;
		try{
			tagger = SenFactory.getStringTagger(null);
		}catch(OutOfMemoryError e){
			// retry
			Runtime.getRuntime().freeMemory();
			tagger = SenFactory.getStringTagger(null);
		}
		List<Token> tokens = new ArrayList<Token>();
		try{
			tagger.analyze(content, tokens);
		}catch(IOException e){
			return null;
		}

		TermList result = new TermList();
		for(int i = 0; i < tokens.size(); i++){
			result.add(new Term(tokens.get(i).getSurface()));
		}

		return result;
	}
}
